﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Qiang.Topology.Geometries;

namespace Qiang.Data
{
    public class Part : Shape,IPart
    {

        #region 初始化


        public Part()
        {
            _segmentCollection = new List<DirectionalSegment>();
        }


        #endregion


        #region 属性

        /// <summary>
        /// shape类型.
        /// </summary>
        public override ShapeType ShapeType
        {
            get { return ShapeType.Part; }
        }


        private List<DirectionalSegment> _segmentCollection;

        /// <summary>
        /// 构成多边形的指向性弧段集合
        /// </summary>
        public List<DirectionalSegment> SegmentCollection
        {
            get { return _segmentCollection; }
            set { _segmentCollection = value; }
        }
         

        private List<ISegmentShape> _segments;

        /// <summary>
        /// 构成多边形的所有弧段
        /// </summary>
        public List<ISegmentShape> Segments
        {
            get
            {
                _segments = this.GetSegments();
                return _segments;
            }
        }

     
        #endregion


        #region 私有方法

        /// <summary>
        /// 获取构成多边形的所有弧段
        /// </summary>
        /// <returns></returns>
        private List<ISegmentShape> GetSegments()
        {
            List<ISegmentShape> segments = new List<ISegmentShape>();
            foreach (DirectionalSegment segment in this.SegmentCollection)
            {
                segments.Add(segment.Segment);
            }
            return segments;
        }

        
        #endregion


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override IList<Coordinate> ComputeCoordinates()
        {
            List<Coordinate> coors = new List<Coordinate>();
            foreach (DirectionalSegment Ds in _segmentCollection)
            {
                if (Ds.Derection == SegmentDirection.Left)
                {
                    coors.AddRange(Ds.Segment.Coordinates);
                }
                else
                {
                    coors.AddRange(Ds.Segment.Coordinates.Reverse());
                }
            }
            return coors;
        }


        protected override IExtent ComputeExtent()
        {
            return base.ComputeExtent();
        }


        public override IGeometry ToGeometry()
        {
            ILinearRing ring = new LinearRing(Coordinates.ToArray());
            return ring;
        }

        public IPolygon ToPolygon()
        {
            IPolygon polygon = new Polygon(Coordinates.ToArray());
            return polygon;
        }

    }
}
